use std::mem;

pub struct Queue{
    ///前台使用的队列
    front: Vec<u32>,

    capacity: usize,
}

impl Queue{
    pub fn with_capacity(capacity: usize) -> Queue{
        let front = Vec::<u32>::with_capacity(capacity);
        Queue{
            front,
            capacity
        }
    }

    pub fn push(&mut self, value: u32){
        self.front.push(value);
    }

    ///take之后，做一次内存复制操作，将队列所有数据前移一位
    pub fn take(&mut self) -> Option<u32>{
        let mut value = None;
        let length = self.front.len();
        if length == 0{
            return value;
        }
        if length == 1{
            let id = self.front.remove(0);
            value = Some(id);
        }
        if length > 1{
            let id = self.front.remove(0);
            value = Some(id);
            let mut swap = Vec::<u32>::with_capacity(length);
            swap.extend_from_slice(&self.front[1..length]);
            mem::swap(&mut self.front, &mut swap);
        }
        value
    }
}